
package org.gis.mvc.dao;

import java.util.List;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 *
 * @author gus
 */
public class ConfigBeanDAOImpl extends JdbcDaoSupport implements ConfigBeanDAO
{
    private final static String CONFIGURATION_TABLE = "configBean";
    
    public String  getXmlConfiguration(String UUID)
    {
        List<String> list = getJdbcTemplate().queryForList(
            "SELECT xml FROM " + CONFIGURATION_TABLE + " WHERE uid = ? LIMIT 1",
            new Object[]{UUID}, String.class);
        
        if( list.isEmpty() )
            return null;
        
        return list.get(0);
    }
    
    public void storeXmlConfiguration(String UUID, String description, String xml)
    {
        int set = getJdbcTemplate().queryForInt(
            "SELECT COUNT(*) FROM " + CONFIGURATION_TABLE + " WHERE uid = ?",
            new Object[]{UUID});
        
        if( set == 0)
        {
            getJdbcTemplate().update("INSERT INTO " 
                    + CONFIGURATION_TABLE 
                    + " (uid, description, xml ) " +
                    " VALUES ( ?, ?, ? )",
                    new String[]{UUID, description, xml});
        } else 
        {
            getJdbcTemplate().update("UPDATE " 
                    + CONFIGURATION_TABLE 
                    + " SET xml = ? , description = ?,  stamp = now() "
                    + " WHERE  uid = ? ",
                    new Object[]{UUID, description, xml});
        }
    }
}
